-- 自动安装 packer.nvim
-- 插件安装目录
-- ~/.local/share/nvim/site/pack/packer
local fn = vim.fn
local install_path = fn.stdpath("data") .. "/site/pack/packer/start/packer.nvim"
local packer_bootstrap

if fn.empty(fn.glob(install_path)) > 0 then
    vim.notify("正在安装packer.nvim, 请稍候...")
    packer_bootstrap = fn.system({
        "git",
        "clone",
        "--depth",
        "1",
        "https://github.com/wbthomason/packer.nvim",
        install_path,
    })

    -- https://github.com/wbthomason/packer.nvim/issues/750
    local rtp_addition = vim.fn.stdpath("data") .. "/site/pack/*/start/*"
    if not string.find(vim.o.runtimepath, rtp_addition) then
        vim.o.runtimepath = rtp_addition .. "," .. vim.o.runtimepath
    end
    vim.notify("pakcer.nvim 安装完毕")
end

-- Use a protected call so we don't error out on first use
local status_ok, packer = pcall(require, "packer")
if not status_ok then
    vim.notify("没有安装 packer.nvim")
    return
end

-- Have packer use a popup window
-- packer.init {
--   display = {
--     open_fn = function()
--       return require("packer.util").float { border = "rounded" }
--     end,
--   },
-- }

-- Install your plugins here
packer.startup({
    function(use)
        -- packer 自升级
        use("wbthomason/packer.nvim")
        -- nvim-tree
        use({
            "kyazdani42/nvim-tree.lua",
            requires = "kyazdani42/nvim-web-devicons"
        })
        -- bufferline
        use({
            "akinsho/bufferline.nvim",
            requires = {"kyazdani42/nvim-web-devicons", "moll/vim-bbye"}
        })
        -- lualine
        use({
            "nvim-lualine/lualine.nvim",
            requires = "kyazdani42/nvim-web-devicons"
        })
        use("arkav/lualine-lsp-progress")
        -- telescope
        use({
            "nvim-telescope/telescope.nvim",
            requires = "nvim-lua/plenary.nvim"
        })
        -- telescope extensions
        use("LinArcX/telescope-env.nvim")
        use("nvim-telescope/telescope-ui-select.nvim")
        -- dashboard-nvim
        use("glepnir/dashboard-nvim")
        -- project
        use("ahmedkhalf/project.nvim")
        -- treesitter
        use({
            "nvim-treesitter/nvim-treesitter",
            run = ":TSUpdate",
        })
        use("p00f/nvim-ts-rainbow")
        use("JoosepAlviste/nvim-ts-context-commentstring")
        -- indent-blankline
        use("lukas-reineke/indent-blankline.nvim")

        ------------------- LSP ------------------
        use("williamboman/nvim-lsp-installer") -- simple to use language server installer
        -- lspconfig
        use("neovim/nvim-lspconfig") -- enable LSP
        use("tamago324/nlsp-settings.nvim") -- language server settings defined in json for
        -- 补全引擎
        use("hrsh7th/nvim-cmp") -- The completion plugin
        -- snippet 引擎
        -- use("hrsh7th/vim-vsnip") -- For vsnip users.
        use("L3MON4D3/LuaSnip") -- For luasnip users.
        -- use "SirVer/ultisnips" -- For ultisnips users.
        -- use "dcampos/nvim-snippy" -- For snippy users.

        -- 补全源
        -- use "hrsh7th/cmp-vsnip" -- For vsnip users.
        use("saadparwaiz1/cmp_luasnip") -- For luasnip users.
        -- use "quangnguyen30192/cmp-nvim-ultisnips" -- For ultisnips users.
        -- use "dcampos/cmp-snippy" -- For snippy users.
        use("hrsh7th/cmp-nvim-lsp") -- { name = nvim_lsp }
        use("hrsh7th/cmp-buffer") -- { name = 'buffer' },
        use("hrsh7th/cmp-path") -- { name = 'path' }
        use("hrsh7th/cmp-cmdline") -- { name = 'cmdline' }
        use("hrsh7th/cmp-nvim-lsp-signature-help") -- { name = 'nvim_lsp_signature_help' }
        -- 常见编程语言代码段
        use("rafamadriz/friendly-snippets") -- a bunch of snippets to use
        -- UI增强
        use("onsails/lspkind-nvim")
        use("tami5/lspsaga.nvim")
        -- 代码格式化
        use("mhartington/formatter.nvim")
        use({ "jose-elias-alvarez/null-ls.nvim", requires = "nvim-lua/plenary.nvim" })
        -- TypeScript增强
        use({ "jose-elias-alvarez/nvim-lsp-ts-utils", requires = "nvim-lua/plenary.nvim" })
        -- lua增强
        use("folke/lua-dev.nvim")
        -- json增强
        use("b0o/schemastore.nvim")
        -- rust增强
        use("simrat39/rust-tools.nvim")

        --------------------- colorschemes -----------------------
        -- use "lunarvim/colorschemes" -- A bunch of colorschemes you can try out
        use("lunarvim/darkplus.nvim")
        -- tokyonight
        use("folke/tokyonight.nvim")
        -- OceanicNext
        use("mhartington/oceanic-next")
        -- gruvbox
        use({
            "ellisonleao/gruvbox.nvim",
            requires = { "rktjmp/lush.nvim" },
        })
        -- zephyr
        -- use("glepnir/zephyr-nvim")
        -- nord
        use("shaunsingh/nord.nvim")
        -- onedark
        use("ful1e5/onedark.nvim")
        -- nightfox
        use("EdenEast/nightfox.nvim")

        ------------------------------------------------
        use("akinsho/toggleterm.nvim")
        -- surround
        use("ur4ltz/surround.nvim")
        --  Comment
        use("numToStr/Comment.nvim") -- Easily comment stuff
        -- nvim-autopairs
        use("windwp/nvim-autopairs") -- Autopairs, integrates with both cmp and treesitter
        -- git
        use("lewis6991/gitsigns.nvim")
        -- vimspector
        use("puremourning/vimspector")

        ----------------------------------------------
        use("mfussenegger/nvim-dap")
        use("theHamsta/nvim-dap-virtual-text")
        use("rcarriga/nvim-dap-ui")
        -- use("Pocco81/DAPInstall.nvim")
        -- use("jbyuki/one-small-step-for-vimkind")

        use("j-hui/fidget.nvim")

        use("nvim-lua/popup.nvim") -- An implementation of the Popup API from vim in Neovim
        use("lewis6991/impatient.nvim")
        use("goolord/alpha-nvim")
        use("antoinemadec/FixCursorHold.nvim") -- This is needed to fix lsp doc highlight
        use("folke/which-key.nvim")

        -- Automatically set up your configuration after cloning packer.nvim
        -- Put this at the end after all plugins
        if packer_bootstrap then
            require("packer").sync()
        end
    end,
    config = {
        -- 最大并发数
        max_jobs = 16,
        -- 自定义源
        git = {
            -- default_url_format = "https://hub.fastgit.xyz/%s",
            -- default_url_format = "https://mirror.ghproxy.com/https://github.com/%s",
            -- default_url_format = "https://gitcode.net/mirrors/%s",
            -- default_url_format = "https://gitclone.com/github.com/%s",
        },
        -- display = {
        -- 使用浮动窗口显示
        --   open_fn = function()
        --     return require("packer.util").float({ border = "single" })
        --   end,
        -- },
    },
})
